package com.finalist.dao.impl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import javax.persistence.FlushModeType;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.persistence.criteria.Subquery;

import org.springframework.stereotype.Repository;

import com.finalist.Page;
import com.finalist.Pageable;
import com.finalist.dao.CombinedCommodityDao;
import com.finalist.entity.Attribute;
import com.finalist.entity.Brand;
import com.finalist.entity.CombinedCommodity;
import com.finalist.entity.ProductCategory;
import com.finalist.entity.Promotion;
import com.finalist.entity.Tag;
import com.finalist.entity.Product.OrderType;

@Repository("combinedCommodityDaoImpl")
public class CombinedCommodityDaoImpl extends
		BaseDaoImpl<CombinedCommodity, Long> implements CombinedCommodityDao {

	public Page<CombinedCommodity> findPage(ProductCategory productCategory,
			Brand brand, Promotion promotion, List<Tag> tags,
			Map<Attribute, String> attributeValue, BigDecimal startPrice,
			BigDecimal endPrice, Boolean isMarketable, Boolean isList,
			Boolean isTop, Boolean isGift, Boolean isOutOfStock,
			Boolean isStockAlert, OrderType orderType, Pageable pageable) {
		CriteriaBuilder criteriaBuilder = entityManager.getCriteriaBuilder();
		CriteriaQuery<CombinedCommodity> criteriaQuery = criteriaBuilder
				.createQuery(CombinedCommodity.class);
		Root<CombinedCommodity> root = criteriaQuery
				.from(CombinedCommodity.class);
		criteriaQuery.select(root);
		Predicate restrictions = criteriaBuilder.conjunction();
		if (promotion != null) {
			Subquery<CombinedCommodity> subquery1 = criteriaQuery
					.subquery(CombinedCommodity.class);
			Root<CombinedCommodity> subqueryRoot1 = subquery1
					.from(CombinedCommodity.class);
			subquery1.select(subqueryRoot1);
			restrictions = criteriaBuilder.and(restrictions,
					criteriaBuilder.or(criteriaBuilder.exists(subquery1)));
		}
		criteriaQuery.where(restrictions);
		return super.findPage(criteriaQuery, pageable);
	}

	/**
	 * 根据组合商品类型 和商城类型查询组合套餐
	 */
	@Override
	public List<CombinedCommodity> findByType(
			CombinedCommodity combinedCommodity) {
		if (combinedCommodity.getType() == null
				&& combinedCommodity.getIsMiao() == null
				&& combinedCommodity.getIsWei() == null
				&& combinedCommodity.getIsApp() == null) {
			return Collections.<CombinedCommodity> emptyList();
		}
		// 风客旅行组合套餐
		if (combinedCommodity.getIsMiao() != null
				&& combinedCommodity.getIsMiao() == true) {
			String jpql = "select combinedCommodity from CombinedCommodity combinedCommodity where combinedCommodity.type = :type and combinedCommodity.isMiao = :miao ";
			return entityManager.createQuery(jpql, CombinedCommodity.class)
					.setFlushMode(FlushModeType.COMMIT)
					.setParameter("type", combinedCommodity.getType())
					.setParameter("miao", combinedCommodity.getIsMiao())
					.getResultList();
		}
		// 微信商城组合套餐
		if (combinedCommodity.getIsWei() != null
				&& combinedCommodity.getIsWei() == true) {
			String jpql = "select combinedCommodity from CombinedCommodity combinedCommodity where combinedCommodity.type = :type and combinedCommodity.isWei = :wei ";
			return entityManager.createQuery(jpql, CombinedCommodity.class)
					.setFlushMode(FlushModeType.COMMIT)
					.setParameter("type", combinedCommodity.getType())
					.setParameter("wei", combinedCommodity.getIsWei())
					.getResultList();
		}
		// app 组合套餐
		if (combinedCommodity.getIsApp() != null
				&& combinedCommodity.getIsApp() == true) {
			String jpql = "select combinedCommodity from CombinedCommodity combinedCommodity where combinedCommodity.type = :type and combinedCommodity.isApp = :app ";
			return entityManager.createQuery(jpql, CombinedCommodity.class)
					.setFlushMode(FlushModeType.COMMIT)
					.setParameter("type", combinedCommodity.getType())
					.setParameter("app", combinedCommodity.getIsApp())
					.getResultList();
		}
		return Collections.<CombinedCommodity> emptyList();
	}
}
